package com.climber.android.commonres.util

import android.app.Application
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.util.Log
import android.widget.ProgressBar
import com.climber.android.commonres.R
import com.climber.android.commonres.widget.StatusView
import com.climber.android.commonres.widget.hkd.KProgressHUD
import com.climber.android.commonsdk.event.BusProvider
import com.climber.android.commonsdk.event.CommonBusEvent
import com.climber.android.commonsdk.event.StatusDialogType
import io.ganguo.library.klog.KLog
import io.ganguo.library.mvp.util.ToastHelper

/**
 * 界面辅助工具
 *
 *
 * Created by mikes on 17-10-3.
 */
object UIHelper {
    private var progressHud: KProgressHUD? = null
    private var statusHud: KProgressHUD? = null

    @JvmStatic
    fun showUnknownError(e: Exception? = null) {
        if (e != null) {
            KLog.e("iQmiError", Log.getStackTraceString(e))
        }
        BusProvider.getInstance().post(CommonBusEvent.ShowMessageEvent(StatusDialogType.error, msgId = R.string.unknown_error))
    }

    @JvmStatic
    fun showUnknownErrorWithToast(e: Exception? = null) {
        if (e != null) {
            KLog.e("iQmiError", Log.getStackTraceString(e))
        }
        ToastHelper.toastMessage(R.string.unknown_error)
    }

    @JvmStatic
    fun showDataParseError() {
        BusProvider.getInstance().post(CommonBusEvent.ShowMessageEvent(StatusDialogType.error, msgId = R.string.parse_error))
    }

    @JvmStatic
    fun showDataParseErrorWithToast() {
        ToastHelper.toastMessage(R.string.parse_error)
    }

    /**
     * show loading
     */
    @JvmStatic
    fun showLoading(context: Context, msg: String? = null, msgId: Int? = null
                    , autoDismiss: Boolean = false, autoDismissTime: Int = 2000, listener: DialogInterface.OnDismissListener? = null,
                    cancellable: Boolean = true) {
        if(context is Application){
            //application context cannot show dialog.
            return
        }

        if (progressHud?.isShowing == true) {
            return
        }

        progressHud = KProgressHUD.create(context)
                .setStyle(context, KProgressHUD.Style.iLoading)
                .setCustomView(ProgressBar(context))
                .setLabel(msg ?: if (msgId != null) {
                    context.resources.getString(msgId)
                } else {
                    null
                })
                .setBackgroundColor(Color.TRANSPARENT)
                .setAutoDismiss(autoDismiss)
                .setAutoDismissTime(autoDismissTime)
                .setCancellable(cancellable)
                .setDismissListener(listener)
                .setDimAmount(0.5f)
                .show()
    }

    /**
     * hide loading
     */
    @JvmStatic
    fun hideLoading() {
        if (progressHud?.isShowing == true) {
            progressHud?.dismiss()
        }
    }

    @JvmStatic
    fun hideStatus() {
        if (statusHud?.isShowing == true) {
            statusHud?.dismiss()
        }
    }

    fun isStatusDialogShowing(): Boolean = statusHud?.isShowing == true

    @JvmStatic
    fun showStatusDialog(context: Context, statusType: StatusDialogType, statusLabel: String, listener: DialogInterface.OnDismissListener? = null) {
        statusHud?.dismiss()
        statusHud = null

        statusHud = KProgressHUD.create(context)
                .setStyle(context, KProgressHUD.Style.Status)
                .setCustomView(StatusView(context, statusType.name))
                .setLabel(statusLabel)
                .setAutoDismiss(true)
                .setAutoDismissTime(2000)
                .setDismissListener(listener)
                .setCancellable(true)
                .setDimAmount(0.5f)
                .show()
    }
}
